<template>
	<div class="center" style="width: 100%;">
		<el-form label-position="top" :model="psdForm">
			<el-form-item label="请输入原密码"><el-input v-model="psdForm.oldPsd" show-password></el-input></el-form-item>
			<el-form-item label="请输入新密码"><el-input v-model="psdForm.newPsd" show-password></el-input></el-form-item>
			<el-form-item label="请确认新密码"><el-input v-model="psdForm.newPsdAgain" show-password></el-input></el-form-item>
			<el-button type="primary" style="margin: 0 75px;width: 150px;" @click="changePsd">确定修改</el-button>
		</el-form>
	</div>
</template>

<script>
export default {
	data() {
		return {
			psdForm: {
				oldPsd: '',
				newPsd: '',
				newPsdAgain: '',
				id: ''
			}
		};
	},
	methods: {
		changePsd() {
			let that = this;
			if (!that.psdForm.oldPsd) {
				that.$message.error('请输入原密码');
				return;
			}
			if (!that.psdForm.newPsd) {
				that.$message.error('请输入新密码');
				return;
			}
			if (that.psdForm.newPsd != that.psdForm.newPsdAgain) {
				that.$message.error('两次密码不一致');
				return;
			}
			//请求后台修改
			console.log('请求后台修改');
			that.psdForm.id = that.$store.state.userInfo.id
			that.axios
				.post(that.$store.state.serverHost + '/' + (that.$store.state.userInfo.type == 1 ? 'student' : 'administrator') + '/changePassword/', that.psdForm)
				.then(res => {
					console.log(res);
					if(res.data=="修改成功"){
						that.$message.success("修改成功")
						setTimeout(()=>{
							that.$router.go(-1)
						},500)
						return
					}
					that.$message.error("修改失败")
				})
				.catch(e => {
					console.log(e);
					that.$message.error("修改失败")
				});
		}
	}
};
</script>

<style scoped>
form {
	width: 300px;
}
</style>
